/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ventas_total.java
 *
 * Created on 8/03/2011, 12:26:53 PM
 */

package punto_de_venta;

/**
 *
 * @author Nekoi
 */
import java.sql.*;
import java.util.logging.*;
import javax.swing.JOptionPane;
import javax.swing.table.TableModel;

public class Ventas_total extends javax.swing.JFrame {
    private String[][] productos;
static String a2,a1,a0,a3;
    /** Creates new form Ventas_total */
    public Ventas_total() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Mostrar Ventas");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Eliminar Registro");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Volver");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(32, 32, 32)
                        .addComponent(jButton2)
                        .addGap(40, 40, 40)
                        .addComponent(jButton3)))
                .addContainerGap(57, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        Principal_Admin pirn= new Principal_Admin();
        pirn.setVisible(true);
        this.dispose();

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        int renglones=0;
            Connection cc;
            try {
                cc = DriverManager.getConnection("jdbc:mysql://localhost/petit", "root", "");
                  Statement sql=(Statement) cc.createStatement();
                  ResultSet seleccionar=sql.executeQuery("select idventa,idproducto,idusuario from ventas");

while(seleccionar.next()){
renglones++;
}
                  Statement sql1=(Statement) cc.createStatement();
                  ResultSet seleccionar1=sql1.executeQuery("select idventa,idproducto,idusuario from ventas");
                 productos=new String [renglones][3];

                 renglones=0;
while(seleccionar1.next()){
productos[renglones][0]=seleccionar1.getString("idventa");

productos[renglones][1]=seleccionar1.getString("idproducto");

productos[renglones][2]=seleccionar1.getString("idusuario");
//productos[renglones][3]=seleccionar1.getString("preciov");
renglones++;
}

            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
            jTable1.setModel((TableModel) new javax.swing.table.DefaultTableModel(productos, nom1));
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
           int renglones=0;
            Connection cc;
            try {
                cc = DriverManager.getConnection("jdbc:mysql://localhost/petit", "root", "");
                  Statement sql=(Statement) cc.createStatement();
                  ResultSet seleccionar=sql.executeQuery("select idventa,idproducto,idusuario from ventas");

while(seleccionar.next()){
renglones++;
}
                  Statement sql1=(Statement) cc.createStatement();
            sql1.executeUpdate("delete from ventas where idventa = '" + a0+"'");

        JOptionPane.showMessageDialog(null, "El registro ha sido eliminado");


            } catch (SQLException ex) {
                Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            }
            jTable1.setModel((TableModel) new javax.swing.table.DefaultTableModel(productos, nom1));
       
       
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
          int pos=jTable1.getSelectedRow();
         a0= (String) jTable1.getValueAt(pos, 0);
         a1= (String) jTable1.getValueAt(pos, 1);
         a2= (String) jTable1.getValueAt(pos, 2);
       // a3= (String) jTable1.getValueAt(pos, 3);
        System.out.println(a0);
    System.out.println(a1);
    System.out.println(a2);
   /// System.out.println(a3);

    }//GEN-LAST:event_jTable1MouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Ventas_total().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
static String nom1[]={"idventa","idproducto","idusuario"};
}
